import sys


def open_file(file):
    try:
        with open(file) as in_file:
            load_text = in_file.read().strip().split('\n')
            load_text = [x.lower() for x in load_text]
            return load_text
    except IOError as e:
        print('文件{}打开发生{}错误'.format(file, e))
        sys.exit(1)


if __name__ == '__main__':
    word = open_file('2of4brif.txt')
    pali_text = []
    words = set(word)
    for w in words:
        end = len(w)
        rev_word = w[::-1]
        if end > 1:
            for i in range(end):
                if w[i:] == rev_word[:end - i] and rev_word[end - i:] in words:
                    pali_text.append((w, rev_word[end - i:]))
                if w[:i] == rev_word[end - i:] and rev_word[:end - i] in words:
                    pali_text.append((rev_word[:end - i], w))

    pali_sorte = sorted(pali_text)

    print('一共发现{}个回文短句'.format(len(pali_sorte)), file=sys.stderr)

    for first, second in pali_sorte:
        print('{} {}'.format(first, second))
